package de.hsb.ai.agent.TeamDetectiveAgent;

import game.Move;

import java.io.IOException;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;
import java.util.Vector;

import org.jgrapht.Graph;
import org.jgrapht.Graphs;

import de.hsb.ai.agent.AbstractDetective;
import de.hsb.ai.util.map.ScotlandJadeMapHelper;
import de.hsb.ai.util.map.ScotlandJadeMapHelper.ScotlandJadeVertex;
import de.hsb.ai.util.map.ScotlandJadeMapHelper.ScotlandJadeVertex.Type;

public class TeamDetective extends AbstractDetective {

	int turn = 1;
	HuntPreparation hp;
	List<ScotlandJadeVertex> mrx = Graphs.neighborListOf(map, map.get(0));
	List<ScotlandJadeVertex> mrxFuture = Graphs.neighborListOf(map, map.get(0));
	int [] detTickets = tickets;
	
	public TeamDetective() {
		super("td");
	}

	/**
	 * The agent moves when it is hin turn.
	 */
	@Override
	protected Move getMove(List<Move> options) {
		Move m = null;
		// if (turn <=2){
		hp = new HuntPreparation(map, this, options, turn,
				lastKnownFugitivePosition, detTickets);
		m = hp.getMove();
		// }else{
		// hunt = new Hunt(map, lastKnownFugitivePosition, this, options);
		// m = hunt.getMove();
		// findMrX();
		// }
		turn += 1;

		// System.out.println("⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅⌅");
		// System.out.println(getCurrentDetectivePosition(options).getIndex());

		return m;
	}

	/**
	 * Method for getting all positions of detectives.
	 */
	protected Vector<ScotlandJadeVertex> getDetectivePositions() {
		Vector<ScotlandJadeVertex> v = new Vector<ScotlandJadeVertex>();
		for (int i = 1; i < 200; i++) {
			if (map.get(i).getType() == Type.DETECTIVE) {
				v.add(map.get(i));
			}
		}
		return v;
	}

	/**
	 * Gets the position of the detective which turn is.
	 */
	protected ScotlandJadeVertex getCurrentDetectivePosition(List<Move> options) {
		Vector<ScotlandJadeVertex> v = getDetectivePositions();
		int[] array1;
		int[] array2;
		int playerNumber = 0;
		for (int i = 0; i < v.size(); i++) {
			List<ScotlandJadeVertex> list1 = Graphs.neighborListOf(map, v
					.get(i));
			array1 = new int[list1.size()];
			for (int j = 0; j < list1.size(); j++) {
				array1[j] = list1.get(j).getIndex();
			}
			Move m = null;
			ListIterator<Move> it = options.listIterator();
			array2 = new int[options.size()];
			int counter = 0;
			while (it.hasNext()) {
				m = it.next();
				array2[counter] = m.getNode();
				counter++;
			}
			if (compare(array1, array2)) {
				playerNumber = i;
				// for (int j = 0; j < array1.length; j++) {
				// System.out.print(array1[j]);
				// }
				// System.out.println();
				// for (int j = 0; j < array2.length; j++) {
				// System.out.print(array2[j]);
				// }
				// System.out.println();
			}
		}
		return v.get(playerNumber);
	}

	
	/**
	 * Compares if an array is similar to another.
	 */
	private boolean compare(int[] array1, int[] options) {
		boolean[] match = new boolean[options.length];
		for (int i = 0; i < match.length; i++) {
			match[i] = false;
		}
		for (int i = 0; i < options.length; i++) {
			for (int j = 0; j < array1.length; j++) {
				if (options[i] == array1[j]) {
					match[i] = true;
				}
			}
		}
		for (int i = 0; i < match.length; i++) {
			if (match[i] != true) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Gets the type of the station.
	 */
	protected int getStationType(ScotlandJadeVertex s) {
		List<ScotlandJadeVertex> l = Graphs.neighborListOf(map, map.get(s
				.getIndex()));
		int type = 0;
		for (int i = 0; i < l.size(); i++) {
			if (type < map.getEdge(s, l.get(i)).getType()) {
				type = map.getEdge(s, l.get(i)).getType();
			}
		}
		return type;
	}

	

}

// //////NOTES

// System.out.println("⌅⌅⌅⌅⌅⌅");
// int asfd = getStationType(map.get(46));
// System.out.println("xxxxxxxxxxxxxxx"+asfd);
// //where to move
// Move m = null;
// //ListIterator to traverse through the list
// ListIterator <Move> i = options.listIterator();
// // while there are more neighbour nodes print them out
// while(i.hasNext()){
// m = i.next();
// System.out.println("!!!!!!!!!!!!!" + m);
// }
//
// Vector<ScotlandJadeVertex> v = getDetectivePositions();
// for (int j = 0; j < v.size(); j++) {
// System.out.println("Detective Positions "+v.get(j).getIndex());
// }

// ScotlandJadeVertex sjv = map.get(67);
// ScotlandJadeVertex sjv2 = map.get(79);
// System.out.println("DER TYP! "+map.getEdge(sjv,sjv2).getType());

// Necessary Methods:
// spreadOut();
// searchMrX(1);
// predictMrX();
// moveTowards();
// measureDistance(12, 46);
// splitImportantDestinations();
// block();